package com.coszero.utils.utils

import android.content.Context
import android.content.pm.ApplicationInfo
import android.os.Environment
import android.util.Log
import java.io.File
import java.io.FileWriter
import java.io.IOException


/**
 * 打印Log日志
 *
 * @link(com.orhanobut:logger:1.13) Created by zero on 2016/04/02.
 * @version 1
 */
object LogX {
    private const val TAG = "AppLog"

    //控制日志开关,自动判断是否显示日志
    //    private static boolean isOpen = BuildConfig.DEBUG;
    private const val isOpen = true

    private var isLogError = isOpen
    private var isLogFile = isOpen
    private var isLogTime = isOpen
    private var isLogPrint = isOpen
    private const val isLogSystem = isOpen


    /**
     * 控制日志开关
     *
     * @param isOpen true:开，false：关
     */
    fun openLog(isOpen: Boolean) {
        isLogError = isOpen
        isLogFile = isOpen
        isLogTime = isOpen
        isLogPrint = isOpen
    }

    /**
     * 打印Error
     *
     * @param msg
     */
    @JvmStatic
    fun e(msg: String) {
        e(TAG, msg)
    }

    @JvmStatic
    fun e(tag: String?, msg: String) {
        if (isLogError) {
            Log.e(tag, msg)
        }
    }


    /**
     * 打印Warn
     *
     * @param msg
     */
    fun w(msg: String) {
        w(TAG, msg)
    }

    @JvmStatic
    fun w(tag: String?, msg: String) {
        if (isLogError) {
            Log.w(tag, msg)
        }
    }

    /**
     * 打印debug
     *
     * @param msg
     */
    @JvmStatic
    fun d(msg: String) {
        d(TAG, msg)
    }

    @JvmStatic
    fun d(tag: String?, msg: String) {
        if (isLogError) {
            Log.d(tag, msg)
        }
    }


    /**
     * 打印info
     *
     * @param msg
     */
    @JvmStatic
    fun i(msg: String) {
        i(TAG, msg)
    }

    @JvmStatic
    fun i(tag: String?, msg: String) {
        if (isLogError) {
            Log.i(tag, msg)
        }
    }

    /**
     * 打印系统日志
     *
     * @param msg
     */
    fun println(msg: String?) {
        if (isLogSystem) {
            kotlin.io.println(msg)
        }
    }

    /**
     * 打印时间日志
     *
     * @param tag
     * @param msg
     */
    fun timeLog(tag: String?, msg: String) {
        if (isLogTime) {
            Log.i(tag, msg + " : " + System.currentTimeMillis())
        }
    }

    /**
     * 打印日志，并存储为文件
     *
     * @param fileName
     * @param msg
     */
    fun logFile(fileName: String, msg: String) {
        if (isLogFile) {
            d(fileName, msg)
            val fileDir = File(
                Environment.getExternalStorageDirectory(),
                "/mlogs/"
            )
            val logFile = File(fileDir, fileName)

            var fileOutputStream: FileWriter? = null
            try {
                if (!fileDir.exists()) {
                    if (!fileDir.mkdirs()) {
                        return
                    }
                }
                if (!logFile.exists()) {
                    if (!logFile.createNewFile()) {
                        return
                    }
                }
                fileOutputStream = FileWriter(logFile, true)
                fileOutputStream.write(msg)
                fileOutputStream.flush()
            } catch (e: Exception) {
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close()
                    } catch (e: IOException) {
                    }
                }
            }
        }
    }

    /**
     * 打印堆栈日志
     *
     * @param e
     */
    fun printStackTrace(e: Exception) {
        if (isLogPrint) {
            e.printStackTrace()
        }
    }

    /**
     * 是否为debug
     *
     * @param context
     * @return
     */
    fun isApkDebugable(context: Context): Boolean {
        try {
            val info = context.applicationInfo
            return (info.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
        } catch (e: Exception) {
        }
        return false
    }
}
